package com.coderside.fragment.arithmetic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class AnalyseBitFile {
	private boolean[] OrderbitArray;
	private CountDownLatch cdown = new CountDownLatch(1);

	public static void main(String[] args) throws FileNotFoundException {
		// boolean[] bitArray = new boolean[1 << 27];
		// int a = 0x7fffffff;
		// Random random = new Random();
		// System.out.println(random.nextInt(10000 * 10000));
		// try {
		// new CreateBitFile().readFile();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		new AnalyseBitFile().test();
	}

	private void test() {
		new Thread(new ReadAThread()).start();
		new Thread(new ReadBThread()).start();
	}

	private void createFile() throws IOException {
		File bitFile = new File("E:\\B.txt");
		int bitSize = 100000000;
		Random random = new Random();
		FileChannel fcout = new RandomAccessFile(bitFile, "rw").getChannel();
		int bufSize = 1000;
		StringBuffer sub = new StringBuffer();
		for (int i = 0; i < bitSize; i++) {
			sub.append("139" + random.nextInt(bitSize) + "\n");
			if (i > 0 && i % bufSize == 0) {
				fcout.write(ByteBuffer.wrap(sub.toString().getBytes()));
				sub.delete(0, sub.capacity());
				System.out.println(i);
			}
		}

	}

	private void readFile() throws IOException {
		File bitFileA = new File("E:\\A.txt");
		File bitFileB = new File("E:\\B.txt");
		File bitFileC = new File("E:\\C.txt");
		FileChannel fcin = new RandomAccessFile(bitFileA, "r").getChannel();
		FileChannel fcinB = new RandomAccessFile(bitFileB, "r").getChannel();
		int bufSize = 1024;
		ByteBuffer rBuffer = ByteBuffer.allocate(bufSize);
		boolean[] bitArray = new boolean[1 << 28];
		byte[] bs = new byte[bufSize];
		int count = 0;
		StringBuffer sub = new StringBuffer();
		while (fcin.read(rBuffer) != -1) {
			int rSize = rBuffer.position();
			rBuffer.rewind();
			rBuffer.get(bs);
			rBuffer.clear();
			String tempstr = new String(bs, 0, rSize);
			sub.append(tempstr.subSequence(0, tempstr.lastIndexOf("\n")));
			String[] indexStrs = sub.toString().split("\n");
			sub.delete(0, sub.length());
			sub.append(tempstr.substring(tempstr.lastIndexOf("\n") + 1));
			for (int i = 0; i < indexStrs.length; i++) {
				String temp = indexStrs[i].trim();
				if ("".equals(temp)) {
					continue;
				}
				int index = Integer.parseInt(temp.substring(3, temp.length()));
				count++;
				System.out.println(count);
				bitArray[index] = true;
			}
		}

		while (fcinB.read(rBuffer) != -1) {
			int rSize = rBuffer.position();
			rBuffer.rewind();
			rBuffer.get(bs);
			rBuffer.clear();
			String tempstr = new String(bs, 0, rSize);
			sub.append(tempstr.subSequence(0, tempstr.lastIndexOf("\n")));
			String[] indexStrs = sub.toString().split("\n");
			sub.delete(0, sub.length());
			sub.append(tempstr.substring(tempstr.lastIndexOf("\n") + 1));
			for (int i = 0; i < indexStrs.length; i++) {
				String temp = indexStrs[i].trim();
				if ("".equals(temp)) {
					continue;
				}
				int index = Integer.parseInt(temp.substring(3, temp.length()));
				if (bitArray[index]) {
				}
			}
		}
	}

	private boolean[] readFileIo(FileChannel fcin) {
		boolean[] bitArray = new boolean[1 << 27];
		try {
			int bufSize = 1024 * 10;
			ByteBuffer rBuffer = ByteBuffer.allocate(bufSize);
			byte[] bs = new byte[bufSize];
			int count = 0;
			StringBuffer sub = new StringBuffer();
			while (fcin.read(rBuffer) != -1) {
				int rSize = rBuffer.position();
				rBuffer.rewind();
				rBuffer.get(bs);
				rBuffer.clear();
				String tempstr = new String(bs, 0, rSize);
				sub.append(tempstr.subSequence(0, tempstr.lastIndexOf("\n")));
				String[] indexStrs = sub.toString().split("\n");
				sub.delete(0, sub.length());
				sub.append(tempstr.substring(tempstr.lastIndexOf("\n") + 1));
				for (int i = 0; i < indexStrs.length; i++) {
					String temp = indexStrs[i].trim();
					if ("".equals(temp)) {
						continue;
					}
					int index = Integer.parseInt(temp.substring(3, temp
							.length()));
					count++;
					System.out.println(fcin.hashCode() + "-" + count);
					bitArray[index] = true;
				}
			}

		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitArray;
	}

	class ReadBThread implements Runnable {

		public void run() {
			try {
				File bitFileC = new File("E:\\C.txt");
				FileChannel fcout = new RandomAccessFile(bitFileC, "rw")
						.getChannel();

				boolean[] bitArray = null;
				File bitFileA = new File("E:\\B.txt");
				FileChannel fcin = new RandomAccessFile(bitFileA, "r")
						.getChannel();
				bitArray = readFileIo(fcin);
				cdown.await();
				int len = bitArray.length;

				for (int i = 0; i < len; i++) {
					if (bitArray[i] && OrderbitArray[i]) {
						fcout.write(ByteBuffer.wrap(("139" + i).getBytes()));
					}
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	class ReadAThread implements Runnable {
		public ReadAThread() {

		}

		public void run() {
			try {
				boolean[] bitArray = null;
				File bitFileA = new File("E:\\A.txt");
				FileChannel fcin = new RandomAccessFile(bitFileA, "r")
						.getChannel();
				bitArray = readFileIo(fcin);
				OrderbitArray = bitArray;
				cdown.countDown();
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

	}
}
